<?
  class Menu
  {
    var $aMenu;
    var $cSelected;
	var $cTarget;
	var $cClass;
	var $cFiltroMenu = '';
	var $cClassContainer;
	var $bMuestraSelector;
	var $cStyleSelected = " class='active' ";
	var $cKeyName ;
	var $cUrlRefresh;

	function setUrlRefresh( $cUrlRefresh )
    {
      $this->cUrlRefresh = $cUrlRefresh;
    }
	function getValue()
    {
      return $this->cSelected;
    }
	function setValue( $cValue )
    {
      $this->cSelected = $cValue;
    }
	function setKeyName( $cKeyName )
	{
		$this->cKeyName= $cKeyName;
        $this->cSelected = getParam( $this->cKeyName, '1' );
	}
	function setMuestraSelector( $bMuestraSelector = true, $cStyle = '' )
	{
		$this->bMuestraSelector = $bMuestraSelector;
        if ( $cStyle != '' ) $this->cStyleSelected = " style='" . $cStyle . "' ";
	}
	function setClass($cClass )
	{
		$this->cClass = $cClass ;
	}
	function Menu ($cTarget = '', $cClass = 'menuitem', $cClassContainer = 'menu', $bMuestraSelector = true , $cKeyName = '')
	{
		$this->cTarget = $cTarget;
		$this->cClass = $cClass;
		$this->cFiltroMenu = getParam('cFiltroMenu');
		$this->cClassContainer = $cClassContainer;
		$this->bMuestraSelector = $bMuestraSelector;
		$this->cKeyName = $cKeyName== '' ? PARAM_MENU : $cKeyName;
        $this->cSelected = getParam( $this->cKeyName , '1');
        //$this->cUrlRefresh = '?' . PARAM_REFRESH . '=menu';
		if ( $bMuestraSelector )
		{
          $_SESSION[$this->cKeyName] = $this->cSelected ;
		} 
	}
  // $isAllowed, $cItem, $cUrl, $cTarget, $cDescription, $bExpand = false , $cCssProperties
  function setMatrix( &$aMatrix )
    {
      $this->aMenu = &$aMatrix;
    }
  function addMatrix( &$aMatrix )
    {
      $this->aMenu = array_merge( (array)$this->aMenu , (array)$aMatrix );
    }
  function &addSubItem( &$oItem, $cItem, $cUrl, $cTarget, $cDescription, $bExpand = false , $cCssProperties, $cEvents, $nTipoLink, $cPath = '')
    {
        $oSubItem = array( true, $cItem, $cUrl, $cTarget, $cDescription, $bExpand , null, $cCssProperties, $cEvents, $nTipoLink, $cPath) ;
        $oItem[6][] =& $oSubItem ;
        return $oSubItem;
    }
  function &addItem ( $cItem, $cUrl, $cTarget, $cDescription, $bExpand = false , $cCssProperties, $cEvents, $nTipoLink, $cPath = '')
    {
		$oItem = array( true, $cItem, $cUrl, $cTarget, $cDescription, $bExpand , null, $cCssProperties, $cEvents, $nTipoLink, $cPath) ;
        $this->aMenu[] =& $oItem ;
        return $oItem;
    }
  function make ( $nOption = MENU_DEFAULT )
  {
    if ( $nOption == MENU_AJAX ) $this->makeHTMLAjax();
    if ( $nOption == MENU_HTML ) $this->makeHTML();
    if ( $nOption == MENU_JAVASCRIPT ) $this->makeJS();
  }

  function makeJS ()
    {
      if ( ! is_array ( $this->aMenu ) ) return false;
      /// Principio
      echo "<script language=\"javascript\" type=\"text/javascript\">";
      echo "demoMenu = [\n";
      /// Inicializa el ciclo
      $nIndex = 0;
      $aParent = array();
      $nNivel = 0;
      $oParent = $this->aMenu;
      $oItem = $oParent[$nIndex++];
      /// Recorre el Arbol
      while ( $oItem || $nNivel > 0)
      {
        if ( $oItem )
        {
        /// Muestra el Item actual
          echo "['','" . $oItem[1]. "','" . $oItem[2] . "','" . $oItem[3] . "','" . $oItem[4] . "',";
        } else
        {
           echo "],\n";
        }
        /// Si tiene hijos => baja un nivel
          if ( $oItem[6] )
          {
            $aParent[] = array($oParent, $nIndex  );
            $nNivel++;
            $oParent = $oItem[6];
            $nIndex = 0;
          } else
          {
            echo "],\n";
          }
        /// Si es el ultimo => sube un nivel
          if ( $nIndex >= count( $oParent ) )
          {
            list( $oParent, $nIndex ) = array_pop( $aParent );
            $nNivel--;
            echo " ],\n";
          }
        /// Se mueve al siguiente
          $oItem = $oParent[$nIndex++];
      }
      /// Final
      echo "];</script>";
      echo "<div id=\"demo1\" ></div>";
      echo "<script language=\"javaScript\" type=\"text/javascript\">";
      echo "cmDraw ('demo1', demoMenu, 'vbr', cmThemeIE, 'ThemeIE');";
      echo "</script>";
    }
  function filterMatrix( &$aMenu, $bMostrarAnidado = false )
  {
      $nqItems = 0;
      for( $nIndex = 0 ; $nIndex < count($aMenu) ; $nIndex++ )
      {
          /// Si tiene hijos (y debe bajar - esto es para que no se recorra todo el arbol) => baja un nivel
          if ( $aMenu[$nIndex][6] )
          {
            $bAnidar= ( $bMostrarAnidado || ! (stripos( $aMenu[$nIndex][1], $this->cFiltroMenu) === FALSE )) ? true: false;
            $nqSubItems = $this->filterMatrix( $aMenu[$nIndex][6], $bAnidar );
            $aMenu[$nIndex][0] = $aMenu[$nIndex][5] = $nqSubItems > 0 ? true : false;
            $nqItems  += $nqSubItems;
            if ( $bMostrarAnidado || ! (stripos( $aMenu[$nIndex][1], $this->cFiltroMenu) === FALSE ))
            {
              $nqItems++;
              $aMenu[$nIndex][0] = true;
              $aMenu[$nIndex][5] =true ;
              // Muestra los hijos

            }
          } else
          {
            if ( $bMostrarAnidado || !(stripos( $aMenu[$nIndex][1], $this->cFiltroMenu) === FALSE) )
              $nqItems++;
            else
              $aMenu[$nIndex][0] = false;
          }
      }
      return $nqItems;
  }
  function makeHTMLAjax()
  {
    return $this->makeHTML( 1 );
  }
  function makeHTML( $nTipoDefault = 4 )
    {
      if ( ! is_array ( $this->aMenu ) ) return false;
      // Filtra la Matriz
      if ( $this->cFiltroMenu != '' )   $this->filterMatrix( $this->aMenu );
      /// Principio
      $cUrlBase = replaceString( $this->cKeyName, '', getThisUrl() );
      //echo "<a accesskey='u'>Men<u>u</u></a>";
      echo "<ul class=\"" . $this->cClass ."\">";
      /// Inicializa el ciclo
      $nIndex = 0;
      $aParent = array();
      $nNivel = 0;
      $oParent = $this->aMenu;
      $oItem = $oParent[$nIndex++];
      $cParentPath = "";
      $bExpand = true;
      /// Recorre el Arbol
      while ( $oItem || $nNivel > 0)
      {
        $cPath = $oItem[10] != '' ? $oItem[10]: $cParentPath . $nIndex . '.' ;
		$cHRef = "javascript:void(0);";
		$cOnClick = "";
        if ( $oItem )
        {
          /// Si es un item para mostrar (es parte del Path o es un camino expandido )
          if ( $bExpand == true || $cParentPath == substr($this->cSelected, 0, strlen($cParentPath) ) )
          {
		  	// Si esta seleccionado => usa la class seleccio
		  	$cClass = ($this->bMuestraSelector && $this->cSelected == $cPath ) ? $this->cStyleSelected :'';
			// Si es un grupo de items  o muestra el menu seleccionado => Pone hace un refresh del menu
			if ( $this->cClassContainer && $this->cUrlRefresh && ($oItem[6] || $this->bMuestraSelector) )
              {
                  $cUrl = $this->cUrlRefresh . '&amp;' . $this->cKeyName . '=' . $cPath;
                  $cOnClick = "send(\"$cUrl\",refreshElement, \"" . $this->cClassContainer . "\", false ); ";
              }
            //Si tiene una URL y tiene acceso => lo agrega al HRef
            if ( $oItem[2]  )
              {
				$cTarget = $oItem[3] ? $oItem[3] : $this->cTarget;
				$cUrl = $oItem[2] . '&amp;' . PARAM_TARGET . '=' . $cTarget. '&amp;' . $this->cKeyName . '=' . $cPath;
                if ( $oItem[9]  ) 
                  $nTipo = $oItem[9] ;
                else
                {
                  $nTipo = $nTipoDefault;
                  if ( $cTarget == 'div_popup' ) $nTipo = 2;
                  elseif ( substr($cTarget, 0, 7)  == 'iframe_'  )  $nTipo = 3;
                  elseif ( substr($oItem[2], 0, 7)  == 'http://'  ) $nTipo = 4;
                  //elseif ( defined(MODO_ADMIN) &&  MODO_ADMIN ) $nTipo = 1;
                }
                switch( $nTipo )
                {
                  case 1: // Ajax
                    $cOnClick .= "send(\"$cUrl\",refreshElement, \"" . $cTarget . "\", true, true, \"". $oItem[1] ."\"  ); return false;";break;
                  case 2:  // Popup
                    $cOnClick = "openPopup( \"$cUrl\" ,\" \", 200, 300 ); "; break;
                  case 3: // IFrame
                    $cOnClick =  "oF = document.getElementById( \"iframe_body_main\"); oF.src=\"$cUrl\"; return false; ";break;
                  case 4: //
                    $cUrl = replaceString( PARAM_REFRESH, '', $cUrl  );
					$cHRef = $cUrl;
                    if ( $oItem[3]  == '_blank') $cHRef  .= "' target='_blank"  ;
                    break;
				}
              }
              if ( $cOnClick ) $cOnClick = "onclick='$cOnClick '  " ;
			if ( $oItem[0]  ) echo "<li  ". $oItem[8] . ($oItem[7] ? " style='" . $oItem[7] . "' ": '') . "><a  $cClass href='$cHRef' $cOnClick ><span>" . $oItem[1] . "</span></a></li>";
            /// Si tiene hijos (y debe bajar - esto es para que no se recorra todo el arbol) => baja un nivel
            if ( $oItem[6] && ( $oItem[5] == true || $cPath == substr($this->cSelected, 0, strlen($cPath) ) ) )
            {
              echo "<ul>";
              $aParent[] = array($oParent, $nIndex , $cParentPath , $bExpand );
              $nNivel++;
              $oParent = $oItem[6];
              $cParentPath = $cPath ;
              $bExpand = $oItem[5];
              $nIndex = 0;
            } //else echo "</li>";
          }
        }
        /// Si es el ultimo => sube un nivel
          if ( $nIndex >= count( $oParent ) )
          {
            list( $oParent, $nIndex , $cParentPath, $bExpand ) = array_pop( $aParent );
            $nNivel--;
			if ( $oParent ) echo "</ul></li>";
          }
        /// Se mueve al siguiente
          $oItem = $oParent[$nIndex++];
      }
      /// Final
      echo "</ul>";
    }
/*
  function makeHTML ( )
    {
      if ( ! is_array ( $this->aMenu ) ) return false;
      /// Principio
      $cUrl = replaceString( $this->cKeyName, '.', getThisUrl() );
      echo "<ul class=\"" . $this->cClass ."\">";
      /// Inicializa el ciclo
      $nIndex = 0;
      $aParent = array();
      $nNivel = 0;
      $oParent = $this->aMenu;
      $oItem = $oParent[$nIndex++];
      $cParentPath = "";
      $bExpand = true;
      /// Recorre el Arbol
      while ( $oItem || $nNivel > 0)
      {
        $cPath = $oItem[10] != '' ? $oItem[10]: $cParentPath . $nIndex . '.' ;
        if ( $oItem )
        {
          /// Si es un item para mostrar (es parte del Path o es un camino expandido )
          if ( $bExpand == true || $cParentPath == substr($this->cSelected, 0, strlen($cParentPath) ) )
          {
          /// Muestra el Item actual
            $cUrl = ( $oItem[2]  ) ? $oItem[2] : getThisUrl() ;
            $cUrl = replaceString( $this->cKeyName , $cPath, $cUrl );
            if ( $oItem[0]  ) echo "<li><a href='$cUrl' ><span>" . $oItem[1] . "</span></a></li>\n";
            /// Si tiene hijos (y debe bajar esto es para que no se recorra todo el arbol) => baja un nivel
            if ( $oItem[6] && ( $oItem[5] == true || $cPath == substr($this->cSelected, 0, strlen($cPath) ) ) )
            {
              echo "<ul>";
              $aParent[] = array($oParent, $nIndex , $cParentPath , $bExpand );
              $nNivel++;
              $oParent = $oItem[6];
              $cParentPath = $cPath ;
              $bExpand = $oItem[5];
              $nIndex = 0;
            } //else echo "</li>";
          }
        }
        /// Si es el ultimo => sube un nivel
          if ( $nIndex >= count( $oParent ) )
          {
            list( $oParent, $nIndex , $cParentPath, $bExpand ) = array_pop( $aParent );
            $nNivel--;
            if ( $oParent ) echo "</ul></li>";
          }
        /// Se mueve al siguiente
          $oItem = $oParent[$nIndex++];
      }
      /// Final
      echo "</ul>";
    }
    */
  }
?>